{% extends 'xadmin/base.html' %}
{% load staticfiles %}
{% load node_tag %}

{% block title %}
    用户
{% endblock %}

{% block css %}
    <link href="{% static 'xadmin/css/plugins/toastr/toastr.min.css' %}" rel="stylesheet">
    <link href="{% static 'xadmin/css/bootstrap-switch.css' %}" rel="stylesheet">
{% endblock %}

{% block main %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <form action="" method="get" class="form-inline col-lg-3" style="padding-left:40px;">{% csrf_token %}
                <input type="hidden" value="{{ siteid }}" name="siteid">
                <div class="form-group">
                    <label>搜索</label>
                    <input type="text" class="form-control input-sm" id="keywords" name="keyword" value="{{ keyword }}" placeholder="邮箱,用户名,手机号">
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-outline btn-default" style="margin-bottom: 0">搜索</button>
                </div>
            </form>
            <div class="col-lg-3 col-lg-offset-6">
                {% if request.user.is_superuser %}
                    <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'groupadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>新增角色</button>
                    <button type="button" class="btn btn-xs btn-danger" onclick="users_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>
                {% else %}
                    {% for perm_type, perm_slut_list in perm_dic.items %}
                        {% if perm_type == 'usergroup' %}
                            {% if 'add' in perm_slut_list %}
                                <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'groupadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>新增角色</button>
                            {% endif %}
                            {% if 'delete' in perm_slut_list %}
                                <button type="button" class="btn btn-xs btn-danger" onclick="users_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>
                            {% endif %}
                        {% endif %}
                    {% endfor %}

                {% endif %}

            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content" style="border-width:0px 0px">
                        <div class="table-responsive">
                            <table class="table table-striped  table-hover">
                                <thead style="background: rgba(237, 240, 245, 0.5);">
                                    <tr>
                                        <th>
                                            <div class="icheckbox_square-green " style="position: relative;">
                                                <input type="checkbox" class="i-checks checkall" name="input[]" style="position: absolute; opacity: 0;">
                                            </div>
                                        </th>
                                        <th>名称</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for g in groups %}
                                        <tr>
                                            <td>
                                                <div class="icheckbox_square-green" style="position: relative;">
                                                    <input type="checkbox" class="i-checks" name="input[]" value="{{ g.id }}" style="position: absolute; opacity: 0;">
                                                </div>
                                            </td>
                                            <td>{{ g.name }}</td>
                                            <td>
                                                {% if request.user.is_superuser %}
                                                    <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'groupedit' %}?siteid={{ siteid }}&groupid={{ g.id }}">编辑</a>
                                                    <a  class="btn btn-xs btn-outline btn-warning" onclick="group_del({{ g.id }})">删除</a>
                                                {% else %}
                                                    {% for perm_type, perm_slut_list in perm_dic.items %}
                                                        {% if perm_type == 'usergroup' %}
                                                            {% if 'update' in perm_slut_list %}
                                                                <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'groupedit' %}?siteid={{ siteid }}&groupid={{ g.id }}">编辑</a>
                                                            {% endif %}
                                                            {% if 'delete' in perm_slut_list %}
                                                                <a  class="btn btn-xs btn-outline btn-warning" onclick="group_del({{ g.id }})">删除</a>
                                                            {% endif %}
                                                        {% endif %}
                                                    {% endfor %}
                                                {% endif %}

                                            </td>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div id="pagnitor" style="margin-left:40px;"></div>
        </div>
    </div>

{% endblock %}

{% block script %}
    <script src="{% static 'xadmin/js/bootstrap-switch.js' %}"></script>
    <script src="{% static 'xadmin/js/plugins/toastr/toastr.min.js' %}"></script>
    <script src="{% static 'xadmin/js/myjs/toastr_customer.js' %}"></script>
    <script>
        //复选框初始化
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green dcheckbox',
            radioClass: 'iradio_square-green',
        });
        //全选事件
        $('.checkall').on('ifChecked', function(event){
            $('.i-checks:not(:first)').iCheck('check');
        });
        $('.checkall').on('ifUnchecked', function(event){
            $('.i-checks:not(:first)').iCheck('uncheck');
        });

        //开关组件初始化
        $("[name='isactive']").bootstrapSwitch({
            'size': 'mini',
        });


        //激活按钮
        $("[name='isactive']").on('switchChange.bootstrapSwitch',function(event,state){
            var id = $(this).attr('id');
            var isactive = 0;
            if(state){
                isactive = 1;
            }
            var data = {'id': id, 'isactive': isactive, 'csrfmiddlewaretoken': '{{ csrf_token }}'};
            $.ajax({
                url: '{% url 'articlestate' %}',
                type: 'post',
                dataType: 'json',
                data: data,
                success: function(bk){

                }
            })
            //内置对象、内置属性
           // console.log(event);
            //获取状态
            //console.log(state);
        });


        function group_del(groupid){
            layer.confirm('要删除这个角色吗?', function(index){
                layer.close(index);
                $.ajax({
                    url: '{% url 'groupdel' %}',
                    type: 'post',
                    dataType: 'json',
                    data: {'groupid': groupid, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                    success: function(bk){
                        if(bk.status == '200'){
                            send_success_message(bk.msg);
                        }else{
                            toastr.error(bk.msg);
                        }
                    }
                })
            })
        }
        //批量删除
        function articles_del(){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                layer.confirm('将删除选中的文章, 是否继续?', {icon: 3}, function(index){
                    layer.close(index);
                    $.ajax({
                        url: '{% url 'articlesdel' %}',
                        type: 'post',
                        dataType: 'json',
                        data: {'ids': id_list.join(','), 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                        success:function(bk){
                            if(bk.status == '200'){
                                send_success_message(bk.message);
                            }
                            else{
                                toastr.error(bk.message);
                            }
                        }
                    });
                })
            }
            else{
                toastr.error('请选择要删除的文章');
            }
        }


    </script>

{% endblock %}